<template>
  <el-dialog
    :model-value="props.visible"
    @update:model-value="(value: boolean) => emit('update:visible', value)"
    :title="dialogTitle"
    width="80%"
    :before-close="handleClose"
  >
    <div class="resume-history-content">
      <!-- 学生信息 -->
      <div class="student-info" v-if="studentInfo">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="学生姓名">{{ studentInfo.student_name }}</el-descriptions-item>
          <el-descriptions-item label="学号">{{ studentInfo.student_number }}</el-descriptions-item>
          <el-descriptions-item label="简历总数">{{ studentInfo.total_resumes }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 简历列表 -->
      <div class="resume-list" v-if="resumeList.length > 0">
        <el-collapse v-model="activeNames" accordion>
          <el-collapse-item 
            v-for="(resume, index) in resumeList" 
            :key="resume.id"
            :title="`${resume.title} (${resume.resume_type})`"
            :name="resume.id"
          >
            <template #title>
              <div class="resume-title">
                <span class="title">{{ resume.title }}</span>
                <el-tag :type="getStatusType(resume.current_status)" size="small">
                  {{ resume.current_status }}
                </el-tag>
                <span class="completion">完成度: {{ resume.completion_rate }}%</span>
              </div>
            </template>

            <!-- 简历基本信息 -->
            <div class="resume-basic-info">
              <el-descriptions :column="2" border size="small">
                <el-descriptions-item label="简历类型">{{ resume.resume_type }}</el-descriptions-item>
                <el-descriptions-item label="当前状态">
                  <el-tag :type="getStatusType(resume.current_status)">
                    {{ resume.current_status }}
                  </el-tag>
                </el-descriptions-item>
                <el-descriptions-item label="完成度">{{ resume.completion_rate }}%</el-descriptions-item>
                <el-descriptions-item label="提交次数">{{ resume.total_submissions }}</el-descriptions-item>
                <el-descriptions-item label="创建时间">{{ formatTime(resume.created_at) }}</el-descriptions-item>
                <el-descriptions-item label="更新时间">{{ formatTime(resume.updated_at) }}</el-descriptions-item>
              </el-descriptions>
            </div>

            <!-- 版本历史 -->
            <div class="version-history" v-if="resume.versions && resume.versions.length > 0">
              <h4>版本历史</h4>
              <el-timeline>
                <el-timeline-item
                  v-for="version in resume.versions"
                  :key="version.version_number"
                  :timestamp="formatTime(version.submitted_at)"
                  placement="top"
                >
                  <el-card>
                    <div class="version-header">
                      <h5>版本 {{ version.version_number }}：{{ version.title }}</h5>
                      <el-tag :type="getStatusType(version.status)" size="small">
                        {{ version.status }}
                      </el-tag>
                    </div>
                    
                    <div class="version-details">
                      <p><strong>提交时间：</strong>{{ version.submitted_at ? formatTime(version.submitted_at) : '未提交' }}</p>
                      <p v-if="version.submission_note"><strong>提交说明：</strong>{{ version.submission_note }}</p>
                      
                      <!-- 审核信息 -->
                      <div v-if="version.review" class="review-info">
                        <el-divider content-position="left">审核信息</el-divider>
                        <el-descriptions :column="1" border size="small">
                          <el-descriptions-item label="审核结果">
                            <el-tag :type="getReviewStatusType(version.review.review_status)">
                              {{ version.review.review_status }}
                            </el-tag>
                          </el-descriptions-item>
                          <el-descriptions-item label="审核人">{{ version.review.reviewer_name }}</el-descriptions-item>
                          <el-descriptions-item label="审核时间">{{ formatTime(version.review.review_date) }}</el-descriptions-item>
                          <el-descriptions-item label="审核建议">
                            <div class="review-comments">{{ version.review.review_comments }}</div>
                          </el-descriptions-item>
                        </el-descriptions>
                      </div>
                      
                      <!-- 文件下载 -->
                      <div v-if="version.file_url" class="file-download">
                        <el-button 
                          type="primary" 
                          size="small" 
                          @click="handleDownload(version)"
                        >
                          <el-icon><Download /></el-icon>
                          下载文件 ({{ version.file_name }})
                        </el-button>
                      </div>
                    </div>
                  </el-card>
                </el-timeline-item>
              </el-timeline>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <el-empty description="暂无简历记录" />
      </div>
    </div>

    <template #footer>
      <el-button @click="handleClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Download } from '@element-plus/icons-vue'
import { formatTime } from '@/utils/format'
import { resumeApi } from '@/api/resume'

interface Props {
  visible: boolean
  studentId?: number
  studentName?: string
}

interface Emits {
  (e: 'update:visible', value: boolean): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 响应式数据
const activeNames = ref<string[]>([])
const studentInfo = ref<any>(null)
const resumeList = ref<any[]>([])
const loading = ref(false)

// 计算属性
const dialogTitle = computed(() => {
  return studentInfo.value 
    ? `${studentInfo.value.student_name} - 简历历史记录`
    : '简历历史记录'
})

// 监听visible变化
watch(() => props.visible, (newVal) => {
  if (newVal && props.studentId) {
    fetchResumeHistory()
  }
})

// 方法
const fetchResumeHistory = async () => {
  if (!props.studentId) return

  loading.value = true
  try {
    const response = await resumeApi.getStudentResumeHistory(props.studentId)
    studentInfo.value = response.data
    resumeList.value = response.data.resumes || []
  } catch (error) {
    ElMessage.error('获取简历历史记录失败')
  } finally {
    loading.value = false
  }
}

const handleClose = () => {
  emit('update:visible', false)
}

const handleDownload = (version: any) => {
  if (version.file_url) {
    window.open(version.file_url, '_blank')
  }
}

const getStatusType = (status: string) => {
  const typeMap: Record<string, string> = {
    'draft': 'info',
    'in_progress': 'warning',
    'pending_review': 'warning',
    'approved': 'success',
    'rejected': 'danger',
    'needs_revision': 'info'
  }
  return typeMap[status] || 'info'
}

const getReviewStatusType = (status: string) => {
  const typeMap: Record<string, string> = {
    'approved': 'success',
    'rejected': 'danger',
    'needs_revision': 'warning'
  }
  return typeMap[status] || 'info'
}
</script>

<style lang="scss" scoped>
.resume-history-content {
  .student-info {
    margin-bottom: 24px;
  }

  .resume-list {
    .resume-title {
      display: flex;
      align-items: center;
      gap: 12px;
      width: 100%;

      .title {
        font-weight: 500;
        flex: 1;
      }

      .completion {
        font-size: 12px;
        color: var(--text-color-secondary);
      }
    }

    .resume-basic-info {
      margin-bottom: 20px;
    }

    .version-history {
      h4 {
        margin: 16px 0 12px 0;
        color: var(--text-color-primary);
      }

      .version-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;

        h5 {
          margin: 0;
          color: var(--text-color-primary);
        }
      }

      .version-details {
        p {
          margin: 8px 0;
          color: var(--text-color-regular);
        }

        .review-info {
          margin-top: 16px;

          .review-comments {
            line-height: 1.6;
            color: var(--text-color-primary);
          }
        }

        .file-download {
          margin-top: 12px;
        }
      }
    }
  }

  .empty-state {
    padding: 40px 0;
    text-align: center;
  }
}
</style>
